package xianchengpool;


import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.*;

/**
 * @功能
 * @创建时间 2019/12/26
 * 线程池：一堆线程装在容器里等待执行
 * 不用启动新线程，减少线程启动和关闭次数，减少资源占量，例子不顾新工人，不辞老工人
 * 一个线程池维护两个队列，一个正在执行的队列，一个已经完成的队列
 */
public class T05_ThreadPool {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service1 = new ThreadPoolExecutor(5, 7, 9, TimeUnit.SECONDS, new ArrayBlockingQueue(4)
          ,new ThreadPoolExecutor.DiscardPolicy());
        ExecutorService service = Executors.newFixedThreadPool(5); //execute,submit
        for(int i =0;i<6;i++){ //第6个任务在任务队列里没进线程池
            service.execute(()->{ //这个runable
                try{
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
        }
        service.shutdown(); //线程池正常关闭，所有线程执行完关闭
    //    service.shutdownNow(); //强制关闭线程池
        System.out.println(service);
        System.out.println(service.isTerminated());  //任务是否已经执行完，线程结束完
        System.out.println(service.isShutdown()); //线程池是不是关闭了
        System.out.println(service);
        TimeUnit.SECONDS.sleep(5);
        System.out.println(service.isTerminated());
        System.out.println(service.isShutdown());
        System.out.println(service);
    }
}
